using System;
using System.Collections;
using System.Net;
using System.Net.Sockets;

using NetDns;
using NetDns.Transport;

namespace NetDns
{
	/// <summary>
	/// Summary description for Dns.
	/// </summary>
    public class Resolver
    {
        protected ITransport transport;

        /// <summary>
        /// </summary>
        public Resolver()
        {
        }

        public Resolver(ITransport transport)
        {
            this.transport = transport;
        }

        public ITransport Transport
        {
            get { return this.transport; }
            set { this.transport = value; }
        }

        /// <summary>
        /// The principal look up function, which sends a request message to the given
        /// DNS server and collects a response. This implementation re-sends the message
        /// via UDP up to two times in the event of no response/packet loss
        /// </summary>
        /// <param name="request">The logical request to send to the server</param>
        /// <param name="dnsServer">The IP address of the DNS server we are querying</param>
        /// <returns>The logical response from the DNS server or null if no response</returns>
        public Response Lookup(Request request)
        {
            // check the inputs
            if ( request == null ) throw new ArgumentNullException( "request" );
            //if (dnsServer == null) throw new ArgumentNullException("dnsServer");

            // We will not catch exceptions here, rather just refer them to the caller

            // get the message
            byte[] requestMessage = request.GetMessage();

            // send the request and get the response
            //ITransport transport = new UdpTransport(dnsServer, 53);
            //ITransport transport = new TcpTransport(dnsServer, 53);
            byte[] responseMessage = transport.SendRequest( requestMessage );

            // and populate a response object from that and return it
            return new Response( responseMessage );
        }
    }
}
